<?php

//就近 www.jiujin.me
global $_W, $_GPC;

$operation = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
if ($operation == 'display') {
	ca('commission.ruanwen.view');
	$list = pdo_fetchall("SELECT * FROM " . tablename('ewei_shop_ruanwen') . " WHERE uniacid = '{$_W['uniacid']}' ORDER BY id DESC");
} elseif ($operation == 'post') {
	$id = intval($_GPC['id']);
	if (empty($id)) {
		ca('commission.ruanwen.add');
	} else {
		ca('commission.ruanwen.edit|scommission.ruanwen.view');
	}
	if (checksubmit('submit')) {
		$data = array('uniacid' => $_W['uniacid'], 'title' => trim($_GPC['title']), 'description' => htmlspecialchars_decode($_GPC['description']), 'status' => intval($_GPC['status']), 'createtime' => time());
		if (!empty($id)) {
			pdo_update('ewei_shop_ruanwen', $data, array('id' => $id));
			plog('commission.ruanwen.edit', "修改软文推广 ID: {$id}");
		} else {
			pdo_insert('ewei_shop_ruanwen', $data);
			$id = pdo_insertid();
			plog('commission.ruanwen.add', "修改软文推广 ID: {$id}");
		}
		message('更新软文推广成功！', $this->createPluginWebUrl('commission/ruanwen', array('op' => 'display')), 'success');
	}
	$ruanwen = pdo_fetch("SELECT * FROM " . tablename('ewei_shop_ruanwen') . " WHERE id = '{$id}' and uniacid = '{$_W['uniacid']}'");
} elseif ($operation == 'delete') {
	ca('commission.ruanwen.delete');
	$id = intval($_GPC['id']);
	$ruanwen = pdo_fetch("SELECT id,title  FROM " . tablename('ewei_shop_ruanwen') . " WHERE id = '{$id}' AND uniacid=" . $_W['uniacid'] . "");
	if (empty($ruanwen)) {
		//message('抱歉，软文推广不存在或是已经被删除！', $this->createWebUrl('shop/notice', array('op' => 'display')), 'error');
		message('抱歉，软文推广不存在或是已经被删除！', referer(), 'success');
	}
	pdo_delete('ewei_shop_ruanwen', array('id' => $id));
	plog('commission.ruanwen.delete', "删除软文推广 ID: {$id} 标题: {$notice['title']}");
	message('软文推广删除成功！', referer(), 'success');
}
load()->func('tpl');
include $this->template('ruanwen');